<?php

namespace app\admin\controller\general;

use app\common\controller\Backend;
use think\Config;
use think\Validate;

/**
 * Mail_Manage
 *
 * @icon  fa fa-cube
 */
class Email extends Backend
{
    /**
     * View
     */
    public function index()
    {
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams(null,null);

            $list = db('email')
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

//            $data = $list->items();

            $result = array("total" => $list->total(), "rows" => $list->items(),"extend"=>[]);

            return json($result);
        }
        $this->view->assign('shangjia_tag',__('On Off'));
        return $this->view->fetch();

    }

    /**
     * Add
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            $msg = $this->validate($params,'Email');
            if($msg != 'true')
                $this->error($msg);

            $params['create_time'] = date("Y-m-d H:i:s");
            unset($params['id']);
            db('email')->insertGetId($params);
            $this->success(__('Add Success'));
        }

        $this->view->assign('id',0);
        $this->view->assign('data',[]);
        $this->view->assign('status',-1);
        $this->view->assign('mail_verify_type',1);
        return $this->view->fetch();
    }

    /**
     * Edit
     */
    public function edit($ids = null)
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            $msg = $this->validate($params,'email');
            if($msg != 'true')
                $this->error($msg);

            $row = db('email')->find($params['id']);
            if($params['status'] == 1){
                db('email')->where("id",'>',0)->update(['status'=>-1]);
            }

            db('email')->update($params);

            if($params['status'] == 1){
                $configData = db('config')->where('group','email')->select();
                foreach ($configData as $item){
                    $name = $item['name'];
                    if(isset($params[$name])){
                        $value = $params[$name];
                        db('config')->where('id',$item['id'])->update(['value'=>$value]);
                    }
                }
            }

            $this->success(__('Operation Success'));
        }

        $data = db('email')->find($ids);
        $this->view->assign('id',$ids);
        $this->view->assign('data',$data);
        $this->view->assign('status',$data['status']);
        $this->view->assign('mail_verify_type',$data['mail_verify_type']);
        return $this->view->fetch('add');

    }

    /**
     * Del
     */
    public function del($ids = null)
    {
        if(empty($ids))
            $this->error(__('Error Parame'));

        //在排課系統没有的话就删除
        $ids = explode(',',$ids);
        foreach ($ids as $id){
            db('email')->where('id',$id)->delete();

        }
        $this->success(__('Operation Success'));
    }
    /**
     * Test_Email
     */
    public function test($ids = null)
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            $receiver = $params['receiver'];
            if (!Validate::is($receiver, "email")) {
                $this->error(__('Please input correct email'));
            }

            $configData = db('config')->where('group','email')->select();
            foreach ($configData as $item){
                $name = $item['name'];
                if(isset($params[$name])){
                    $value = $params[$name];
                    db('config')->where('id',$item['id'])->update(['value'=>$value]);
                }
            }

            $email_user = $params['mail_from'];
            $email = new \app\common\library\Email;
            $result = $email
                ->to($receiver)
                ->from($email_user,'SpaceStudio')
                ->subject(__("This is a test mail", config('site.name')))
                ->message('<div style="min-height:550px; padding: 100px 55px 200px;">' . __('This is a test mail content', config('site.name')) . '</div>')
                ->send();


            $usedEmail = db('email')->where('status',1)->find();
            $configData = db('config')->where('group','email')->select();
            foreach ($configData as $item){
                $name = $item['name'];
                if(isset($usedEmail[$name])){
                    $value = $usedEmail[$name];
                    db('config')->where('id',$item['id'])->update(['value'=>$value]);
                }
            }

            if ($result) {
                $this->success(__('Success'));
            } else {
                $this->error($email->getError());
            }
        }

        if(empty($ids))
            $this->error(__('Error Parame'));

        $data = db('email')->find($ids);
        $this->view->assign('id',$ids);
        $this->view->assign('data',$data);
        return $this->view->fetch();

    }
}
